package com.luocj.brvah_srl.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.luocj.brvah_srl.R;
import com.luocj.brvah_srl.adapter.HomeAdapter;
import com.luocj.brvah_srl.evnet.MyEvent;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView recyclerview;
    private ArrayList<String> mDatas;
    private String TAG = HomeActivity.class.getSimpleName();
    private HomeAdapter adpater;
    private ImageView empty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initDatas();
        initView();
    }

    private ArrayList<String> initDatas() {
        mDatas = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            mDatas.add(" itme :" + i);
        }

        return mDatas;
    }

    private void initView() {
        smartRefreshLayout = findViewById(R.id.smartrefreshlayout_home);
        recyclerview = findViewById(R.id.recyclerview_home);
        empty = findViewById(R.id.iv_empty);
        ClassicsHeader header = findViewById(R.id.header);
        ClassicsFooter footer = findViewById(R.id.footer);

        LinearLayoutManager layoutManager = new LinearLayoutManager(HomeActivity.this, LinearLayoutManager.VERTICAL, false);
        recyclerview.setLayoutManager(layoutManager);
        adpater = new HomeAdapter(HomeActivity.this, new ArrayList<String>());
        recyclerview.setAdapter(adpater);
//        View inflate = getLayoutInflater().inflate(R.layout.item_home, (ViewGroup) recyclerview.getParent(), false);
//        adpater.addHeaderView(inflate);


        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.autoRefresh();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refresh(refreshlayout, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refresh(refreshlayout, true);
            }
        });

    }


    private void refresh(RefreshLayout refreshlayout, final boolean isRefresh) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (isRefresh) {
                    adpater.setNewData(initDatas());
                    empty.setVisibility(View.GONE);
                    smartRefreshLayout.finishRefresh();
                    smartRefreshLayout.setLoadmoreFinished(false);
                } else {
                    //加载更多
                    adpater.addData(initDatas());
                    smartRefreshLayout.finishLoadmore();
                }
            }
        }, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().post(new MyEvent("hahahahahah"));
    }
}
